package com.banzhi.shop.activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;


import com.banzhi.shop.MyApp;
import com.banzhi.shop.R;


import com.banzhi.shop.adapter.MyGroupingAdapter;
import com.banzhi.shop.adapter.MyGroupingAdapter2;
import com.banzhi.shop.base.BaseActivity;
import com.banzhi.shop.data.DanduOrderBean;
import com.banzhi.shop.data.DanduOrderBean;
import com.banzhi.shop.data.DanduOrderData;
import com.banzhi.shop.data.PinTuanBean;
import com.banzhi.shop.entity.RefundRequest.Refund_Data;
import com.banzhi.shop.fragment.MyGroupingFragments2;
import com.banzhi.shop.model.CallBack.ConfirmReceiptCallBack;
import com.banzhi.shop.model.CallBack.DanduOrderCallBack;
import com.banzhi.shop.model.CallBack.GroupRefundRequestCallBack;
import com.banzhi.shop.model.Impl.ConfirmReceiptImpl;
import com.banzhi.shop.model.Impl.DanduOrderImpl;
import com.banzhi.shop.model.Impl.GroupRefundRequestImpl;
import com.banzhi.shop.utils.ASUtils;
import com.banzhi.shop.utils.EndlessRecyclerOnScrollListener;
import com.banzhi.shop.utils.FastScrollManger;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.NoTokenUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.banzhi.shop.utils.Translucent;
import com.banzhi.shop.view.VpSwipeRefreshLayout;




import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


public class BuyOrderActivity extends BaseActivity implements DanduOrderCallBack, GroupRefundRequestCallBack, ConfirmReceiptCallBack, SwipeRefreshLayout.OnRefreshListener, MyGroupingAdapter2.OnItemClickListener
{

    private DanduOrderImpl mModel;
    private DanduOrderData mBeans;
    private VpSwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRecyclerView;

    private int page = 1;
    private ImageView mAbck;
    private GroupRefundRequestImpl mRefundRequestModel;
    private Refund_Data mRefund;
    private String mOid;
    private ConfirmReceiptImpl mConfirmReceiptModel;
    private List<DanduOrderBean> mData;
    private String mPrice;
    private List<DanduOrderBean> mPinTuanBeans;
    private List<DanduOrderBean> mMyGroupingBeans = new ArrayList<>();
    private MyGroupingAdapter2 mAdapter1;
    private LinearLayoutManager mLayoutManager;
    private int position;
    private DanduOrderBean mPinTuanBean2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy_order);
        Translucent.setTranslucent(this);
        initView();
        initListener();
        Intent intent = getIntent();
        mPrice = intent.getStringExtra("price");
        mModel = new DanduOrderImpl();
        mModel.getData(page + "", this);
        mRefundRequestModel = new GroupRefundRequestImpl();

        mConfirmReceiptModel = new ConfirmReceiptImpl();


    }

    private void initView() {
        mRefreshLayout = (VpSwipeRefreshLayout) findViewById(R.id.refresh);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        mAbck = (ImageView) findViewById(R.id.iv_back);
}

    @Override
    public void ongetDataSuccess(Object object) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        mPinTuanBeans = (List<DanduOrderBean>) object;
        if (page == 1) {
            mMyGroupingBeans.clear();
        } else {
            if (mPinTuanBeans.size() == 0) {
                return;
            }
        }
        mMyGroupingBeans.addAll(mPinTuanBeans);

      showView();

    }

    private void showView() {
        mAdapter1 = new MyGroupingAdapter2(mMyGroupingBeans);
        mLayoutManager = new LinearLayoutManager(this);

        mRecyclerView.setAdapter(mAdapter1 );
        mAdapter1 .setOnItemClickListener(this);

    }

    private void initListener() {
        FastScrollManger fastScrollManger = new FastScrollManger(this, LinearLayoutManager.VERTICAL, false);
        mRecyclerView.setLayoutManager(fastScrollManger);
        mRecyclerView.addOnScrollListener(new EndlessRecyclerOnScrollListener(fastScrollManger) {
            @Override
            public void onLoadMore() {
                page++;
                LoadingDialogUtils.showProgress(BuyOrderActivity.this, "正在加载,请稍后~");
                mModel.getData(page + "", BuyOrderActivity.this);
            }
        });
        mAbck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        mRefreshLayout.setOnRefreshListener(this);

    }

    @Override
    public void ongetDataError(Object object) {

    }

    @Override
    public void onGroupRefundRequestSuccess(Object object) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        Refund_Data refund = (Refund_Data) object;
        Intent intent2 = new Intent(this, RefundRequestActivity3.class);
        intent2.putExtra("data", refund);
        intent2.putExtra("Orderid",mPinTuanBean2.getOid());
        intent2.putExtra("status","1");
        startActivity(intent2);

    }

    @Override
    public void onGroupRefundRequestError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
        //    NoTokenUtils.loginOut(getActivity(), jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void confirmReceiptSuccess(Object object) {
        if (mRefreshLayout.isRefreshing()) {
            mRefreshLayout.setRefreshing(false);
        }
        JSONObject jsonObject = (JSONObject) object;
        ToastUtils.makeToastShort(jsonObject.optString("msg"));

    }

    @Override
    public void confirmReceiptError(Object object) {
        JSONObject jsonObject = (JSONObject) object;
     //   NoTokenUtils.loginOut(BuyOrderActivity.this, jsonObject);
        ToastUtils.makeToastShort(jsonObject.optString("msg"));
    }

    @Override
    public void onRefresh() {
        //   mConfirmReceiptModel.confirmReceiptData( mOid ,BuyOrderActivity.this);
        mModel.getData(page + "", BuyOrderActivity.this);
        mAdapter1.notifyDataSetChanged();
    }



    private void showConfirmReceiptPopupwindow(final DanduOrderBean pinTuanBean3) {
        //设置contentView
        View contentView = LayoutInflater.from(this).inflate(R.layout.popupwindow_queren_shouhuo, null);
        int width = this.getResources().getDisplayMetrics().widthPixels;
        int height = this.getResources().getDisplayMetrics().heightPixels;

        final PopupWindow popWindow = new PopupWindow(contentView, width, height);
        popWindow.setAnimationStyle(R.style.AnimBottom);

        popWindow.setFocusable(true);
        popWindow.setOutsideTouchable(false);// 设置允许在外点击消失
        //设置各个控件的点击响应
        TextView tvcancle = (TextView) contentView.findViewById(R.id.tv_cancle);
        TextView tvtrue = (TextView) contentView.findViewById(R.id.tv_true);
        RelativeLayout rl = (RelativeLayout) contentView.findViewById(R.id.rl_background);
        LinearLayout ll = (LinearLayout) contentView.findViewById(R.id.ll);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        tvcancle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popWindow.dismiss();
            }
        });
        tvtrue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                mConfirmReceiptModel.confirmReceiptData( pinTuanBean3.getOid() ,BuyOrderActivity.this);
                // setRefreshListener();
                mAdapter1.notifyDataSetChanged();
                popWindow.dismiss();
            }
        });
        popWindow.showAtLocation(contentView, Gravity.CENTER | Gravity.CENTER_HORIZONTAL, 0, 0);
    }

    @Override
    public void onItemClick(View view, int position) {
        switch (view.getId()) {
            case R.id.bt1:
                Intent intent = new Intent(this,BuyOrderDetailsActivity.class);
               intent.putExtra("id",mMyGroupingBeans.get(position).getOid());
                startActivity(intent);
                break;
            case R.id.bt4:   //申请售后
                mPinTuanBean2 = mMyGroupingBeans.get(position);
                mRefundRequestModel.getData(mPinTuanBean2.getOid(),this);
                BuyOrderActivity.this.position = position;
            case R.id.bt6:   //确认收货
                DanduOrderBean pinTuanBean3 = mMyGroupingBeans.get(position);
                showConfirmReceiptPopupwindow(pinTuanBean3);
                break;
            case R.id.bt7:   //售后详情
                DanduOrderBean pinTuanBean5 = mMyGroupingBeans.get(position);
                Intent intent3 = new Intent(this, ShouhouOrderInfoActivity2.class);
                intent3.putExtra("id",pinTuanBean5.getRefundid()+"");
                intent3.putExtra("oid",pinTuanBean5.getOid()+"");
                startActivity(intent3);
                break;
            case R.id.ll_item:
                if ("1".equals(mMyGroupingBeans.get(position).getGoodstatus())) {
                    Intent intent1 = new Intent(this, GroupingDetailsActivity.class);
                    intent1.putExtra("id",mMyGroupingBeans.get(position).getGid());
                    startActivity(intent1);
                }else {
                    ToastUtils.makeToastShort("该拼团商品已经下架。");
                }
        /*     Intent intent1 = new Intent(getActivity(), GroupingDetailsActivity.class);
             intent1.putExtra("id",mMyGroupingBeans.get(position).getGid());
             startActivity(intent1);*/

                break;

        }

    }
}
